import cv2
import numpy as np
import tensorflow as tf
from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2, preprocess_input, decode_predictions

# 加载预训练的MobileNetV2模型
model = MobileNetV2(weights='imagenet')

# 加载图像并进行预处理
def load_and_preprocess_image(image_path):
    img = cv2.imread(image_path)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = cv2.resize(img, (224, 224))
    img = preprocess_input(img)
    return np.expand_dims(img, axis=0)

# 进行图像识别
def predict_image(image_path):
    img = load_and_preprocess_image(image_path)
    preds = model.predict(img)
    print('Predicted:', decode_predictions(preds, top=3)[0])

# 示例图像路径
image_path = './test.png'

# 预测图像中的对象
predict_image(image_path)
